#include "FileManager.h"
#include <iostream>
#include <cstring>

int main()
{
    MiniDB::FileManager &fm = MiniDB::FileManager::getInstance();

    // 创建目录
    fm.createDirectory("test_db");

    // 创建文件
    auto file_handle = fm.createFile("test_db/test_file.dat");
    if (!file_handle)
    {
        std::cout << "Failed to create file" << std::endl;
        return 1;
    }

    // 写入一些数据
    const char *data1 = "Hello, MiniDB!";
    const char *data2 = "This is block 2";

    if (file_handle->appendBlock(data1, strlen(data1) + 1))
    {
        std::cout << "Data 1 appended successfully" << std::endl;
    }

    if (file_handle->appendBlock(data2, strlen(data2) + 1))
    {
        std::cout << "Data 2 appended successfully" << std::endl;
    }

    // 刷新文件
    file_handle->flush();

    // 读取数据
    char buffer[256];

    if (file_handle->readBlock(0, buffer, sizeof(buffer)))
    {
        std::cout << "Block 0 read: " << buffer << std::endl;
    }

    if (file_handle->readBlock(1, buffer, sizeof(buffer)))
    {
        std::cout << "Block 1 read: " << buffer << std::endl;
    }

    // 文件信息
    std::cout << "File size: " << file_handle->getFileSize() << " bytes" << std::endl;
    std::cout << "Block count: " << file_handle->getBlockCount() << std::endl;

    // 关闭文件
    fm.closeFile("test_db/test_file.dat");

    std::cout << "File manager test completed successfully!" << std::endl;
    return 0;
}
